Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wx-axios-promise

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wx-axios-promise

小程序的request库,封装了wx的所有api为Promise,用法和axios类似

  • 2.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by120%
Maintainers
1
Weekly downloads
 
Created
Source

wx-axios-promise

特征

  • 提供和axios相似体验,支持默认参数配置,拦截功能,和create创建新的对象
  • 默认将小程序的api封装成Promise,通过降级,兼容低版本手机系统

使用方法

npm i wx-axios-promise -S

小程序 npm 支持

小程序代码片段测试

import Abi from 'wx-axios-promise'
let api = Abi()

传递相关配置来创建请求(以下参数为默认)

//详情可参考wx.request
let api = Abi({
    url: '',//默认的接口后缀
    method: 'get',//默认的HTTP 请求方法
    dataType: 'json',//默认的返回类型
    responseType: 'text',
    header: {
      'content-type': "application/json"
    }
  })

除上面的创造方法外,我们还可以用实例上的create的方法创建新实例。

let api = Abi()
let newApi = api.create()

请求操作

/**
*默认是get
*如果你设置了默认的url。会自动配置 默认url + url
*如果你的url是http://或者https://开头,那么不会添加默认url
*/
//多种请求方式
api(url, data)
api(SERVER[api], apiData)
api.get(SERVER[api], apiData)
api(SERVER.URL + SERVER[api], apiData)
api(`${SERVER[api]}?page=${apiData.page}&count=${apiData.count}`)
api({
   url: SERVER[api],
   data: apiData,
   <!--method: 'get',-->
   <!--dataType: 'json',-->
   <!--responseType: 'text',-->
   <!--header: {-->
   <!--    content-type': "application/json"-->
   <!--}-->
})

api.post(url, data)
支持
'get',
'post',
'put',
'delete',
'options',
'head',
'trace',
'connect'

可以架起请求、响应、成功、失败的拦截

api.interceptors.response.use(function (config){
  //接口||wx.接口
  return config.data || config
}, function(error){
    return error
})
api.interceptors.request.use(function (config){
  //返回的是和wx.request相关的参数
  console.log(config)
  wx.showLoading({
    title: '加载内容'
  })
}, function(error){
    return error
})

wx全Promise

api.wx.chooseImage()
.then( res => api.wx.uploadFile())
.then()

当然,如果你并不需要这个功能,你也可以在创建的时候设置第二个参数为false

Keywords

FAQs

Package last updated on 18 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc